package com.bootx.controller.api;

import com.bootx.controller.BaseController;
import com.bootx.entity.Tool;
import com.bootx.entity.ToolCategory;
import com.bootx.pojo.CategoryPojo;
import com.bootx.pojo.ToolPojo;
import com.bootx.service.ToolCategoryService;
import com.bootx.service.ToolService;
import com.bootx.util.JsonUtils;
import com.bootx.util.WebUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author black
 */
@RestController
@RequestMapping("/api")
public class IndexController extends BaseController {

   @Resource
    private ToolService toolService;

   @Resource
   private ToolCategoryService toolCategoryService;

    @GetMapping("/loadData")
    public String loadData(){
        Map<String,String> headers = new HashMap<>();
        String s = WebUtils.get("https://api.yanbiai.com/app/templates?app=all", headers, null);
        CategoryPojo object = JsonUtils.toObject(s, new TypeReference<CategoryPojo>() {
        });
        object.getTempates().forEach(t->{
            String path = t.getPath();
            String s1 = WebUtils.get("https://api.yanbiai.com/app/template-detail?id=" + path, headers, null);
            ToolPojo toolPojo = JsonUtils.toObject(s1,new TypeReference<ToolPojo>() {});
            Tool tool = new Tool();
            tool.setPath(path);
            tool.setType(t.getType());
            tool.setDescription(toolPojo.getData().getDescription());
            tool.setMemo(toolPojo.getData().getDesc());
            tool.setImage(toolPojo.getData().getImg());
            tool.setTitle(toolPojo.getData().getTitle());
            tool.setName(toolPojo.getData().getName());
            try {
                tool.setViewCount((long) toolPojo.getData().getView());
            }catch (Exception ignored){

            }
            List<Tool.Form> forms = new ArrayList<>();
            toolPojo.getData().getForm().forEach(f->{
                Tool.Form form = new Tool.Form();
                form.setLabel(f.getLabel());
                form.setOptions(f.getOptions());
                form.setType(f.getType());
                form.setMaxLength(f.getMaxLength());
                form.setIsRequired(f.getIsRequired());
                form.setMaxLength(f.getMaxLength());
                form.setPlaceholder(f.getPlaceholder());
                form.setDefaultValue(f.getDefaultValue());
                form.setAllowCreate(f.getAllowCreate());
                forms.add(form);
            });
            tool.setForms(forms);
           try {
               toolService.save(tool);
               ToolCategory toolCategory = toolCategoryService.create(tool.getType());
               tool.setToolCategory(toolCategory);
               toolService.update(tool);
           }catch (Exception ignored){
           }
        });
        return "ok";
    }


}
